package com.hspedu.method;

/**
 * @author LiuJinTao
 * @className ThreadMethod02
 * @date 2024/06/23 16:50
 * @description TODO
 */
public class ThreadMethod02 {
    public static void main(String[] args) throws InterruptedException {
        T t = new T();
        t.start();
        for (int i = 0; i < 20; i++) {
            if (i == 5) {
                System.out.println("主线程让子线程先吃包子");
                // join: 线程插队，一定会成功
//                t.join();// 相当于让 t 子线程先执行完毕

                // yield: 线程礼让，不一定成功
                Thread.yield();
                System.out.println("子线程吃完了，主线程接着吃");
            }
            Thread.sleep(1000);
            System.out.println("主线程吃了" + i + "个包子");
        }
    }
}

class T extends Thread {

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("子线程吃了" + i + "个包子");
        }
    }
}